package com.lanou.musicbox.fragment.seekmusic;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.seekmusic.EnterTheSeekMusicAdapter;
import com.lanou.musicbox.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by 赵宝龙 on 15/10/24.
 * 进入搜索页面
 */
public class EnterTheSeekMusicFragment extends BaseFragment {
    private TabLayout enterthe_seekmusic_tabLayout;
    private ViewPager enter_seekmusic_viewPage;
    private EnterTheSeekMusicAdapter adapter;
    private String url;
    List<Fragment> data;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.enterthe_seekmusic, null);// 添加布局文件
        // 初始化
        enterthe_seekmusic_tabLayout = (TabLayout) view.findViewById(R.id.enterthe_seekmusic_tabLayout);
        enter_seekmusic_viewPage = (ViewPager) view.findViewById(R.id.enter_seekmusic_viewPage);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 设置颜色
        enterthe_seekmusic_tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.primary_light));
        enterthe_seekmusic_tabLayout.setTabTextColors(getResources().getColor(R.color.gray_bg), getResources().getColor(R.color.white));
        enterthe_seekmusic_tabLayout.setBackgroundColor(getResources().getColor(R.color.accent));
        adapter = new EnterTheSeekMusicAdapter(getFragmentManager());// 初始化adapter
        enter_seekmusic_viewPage.setAdapter(adapter);// 绑定布局
        enterthe_seekmusic_tabLayout.setupWithViewPager(enter_seekmusic_viewPage);// 将tabLayout绑定到viewPage
        Bundle bundle = getArguments();
        url = bundle.getString("url");
        addFragment();
    }

    private void addFragment() {
        data = new ArrayList<>();
        // 添加页面
        data.add(EnterTheSeekMusic_SingleFragment.passData(getActivity(), url));
        data.add(EnterTheSeekMusic_SingerFragment.passData(getActivity(), url));
        data.add(EnterTheSeekMusic_AlbumFragment.passData(getActivity(), url));
        data.add(EnterTheSeekMusic_PlaylistFragment.passData(getActivity(), url));
        data.add(EnterTheSeekMusic_MVFragment.passData(getActivity(), url));
        adapter.setData(data);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
            for (Fragment fragment : data) {
                getFragmentManager().beginTransaction().remove(fragment).commit();
                
        }

    }
}
